<template>
    <!-- Main content -->
    <div>
        <div class="row">
            <div class="col-xs-12">
                <theme-box title="商户结算账户列表">
                    <form class="form-inline" @submit.prevent v-el:form>
                        <form-select title="商户" :up-table="true" :sreach="true" name="mchtId" :options="mchtList"></form-select>
                        <form-select title="结算通道" :sreach="true" :up-table="true" :options="typeOptions"
                                     name="merWay"></form-select>
                        <button type="reset" @click="reset" class="btn btn-warning pull-right">重置</button>
                        <v-button theme="primary" @click="search" class="pull-right">查询</v-button>
                    </form>
                    <table class="table table-bordered table-striped" style="width:100%" v-el:table>
                    </table>
                </theme-box>
            </div>
            <!-- /.col -->
        </div>
        <modal-container title="修改" v-el:revise-modal v-ref:revise-modal class="edit-modal">
            <form @submit.prevent @reset.prevent v-el:revise-mform>
                <div class="modal-body">
                    <table-modal :item="item" :columns-set="columnsReviseSet" :columns-num="1"></table-modal>
                </div>

                <div class="modal-footer">
                    <v-button class="pull--left" data-dismiss="modal" @click="resetBindDrag">关闭</v-button>
                    <v-button theme="primary" type="submit">保存</v-button>
                </div>
            </form>

        </modal-container>
        <!-- /.row -->
    </div>
    <!-- /.content -->
</template>

<style lang="less" scoped>
    .form-inline .btn+.btn {
        margin-right: 10px;
    }
</style>
<script>
    import {
        dateTime,
        createDataTable
    } from 'ui/collections/general.js'
    var serialize = require('form-serialize');
    import {dateFormat} from "lib/method.js"

    var dataTable;
    var columnsSet;
    export default {
        data: () => ({
            item: {},
            columnsSet: [],
            mchtList: [],
            typeOptions:[
                {key: "全部",value: ""},
                {key: "益码通",value: "ebjPay"},
                {key: "易生",value: "easyPay"}
            ],
            columnsReviseSet: [{
                title: "商户号",
                data: "mchtId",
                visible: false,
                readonly: true,
                className: "operate"
            },{
                title: "商户名称",
                data: "mchtName",
                edit: true,
                readonly: true,
                className: "operate"
            }, {
                title: "结算通道",
                data: "merWay",
                className: "operate",
                options:[
                    {key: "益码通",value: "ebjPay"},
                    {key: "易生",value: "easyPay"}
                ],
            }, {
                title: "支付商户id",
                data: "merId",
                edit: true,
                className: "operate"
            }, {
                title: "支付key",
                data: "merKey",
                edit: true,
                className: "operate"
            }]

        }),
        methods: {
            //还原弹出框位置
            resetBindDrag() {
                this.$refs.reviseModal._resetBindDrag();
            },
            //查询按钮
            search: function () {
                dataTable.ajax.reload();
            },
            //重置按钮
            reset: function () {
                $(".form-inline")[0].reset();
                dataTable.ajax.reload();
            },
            getMcht() {
                let self = this;
                var mchtId = JSON.parse(localStorage.getItem("id_token")).mchtId
                self.$post({
                    url: '/api/v2' + projectName_Url + '/list/mcht/' + mchtId,
                    method: "GET"
                }).then(response => {
                    let list = response.content.info;
                    if(list.length == 1){
                        list.unshift({key:"商户",value:""});
                        self.$set("mchtList", list);
                    }else{
                        list.unshift({key:"所有商户",value:""});
                        self.$set("mchtList", list);
                        self.mchtIdValue = list[0].value
                    }
                },  error => {});
            }
        },
        created() {
            //datatable所用数组
            columnsSet = [{
                title: "商户号",
                data: "mchtId",
                visible: false,
                className: "operate"
            },{
                title: "商户名称",
                data: "mchtName",
                className: "operate"
            }, {
                title: "结算通道",
                data: "merWay",
                className: "operate",
                render: function (data) {
                    return (data == "easyPay") ? "易生" : "益码通"
                }
            }, {
                title: "支付商户id",
                data: "merId",
                className: "operate"
            }, {
                title: "支付key",
                data: "merKey",
                className: "operate"
            }, {
                title: "相关操作",
                className: "operate",
                width: "160px",
                render: function (value, type, row) {
                    return `
                            <button type="button" class="btn btn-primary btn-sm revise">修改</button>
                        `
                }
            }]
        },
        ready: async function () {
            $(".dataTable img").css({"width": "50px", "height": "50px"});
            dateTime({
                format: "yyyy-mm-dd",
                minView: "2"
            })
            this.$set("columnsSet", columnsSet)
            var self = this;
            self.getMcht()
            dataTable = $(this.$els.table).DataTable({
                "columns": this.columnsSet, //定义列
                "ajax": async function (data, callback, settings) {
                    var page = [{
                        "name": "iDisplayStart",
                        "value": 1 + data.start / data.length
                    }, {
                        "name": "iDisplayLength",
                        "value": data.length
                    }];
                    //表单序列化数据
                    var _data = serialize(self.$els.form, {
                        hash: true,
                        disabled: true,
                        empty: true
                    });
                    _data.aoData = JSON.stringify(page);
                    //发送请求
                    var result = await self.$post({
                        url: "/api/v2" + projectName_Url + "/plat/mchtSett/query",
                        data: _data,
                        method: "POST"
                    })
                    if (result.respCode != "00") {
                        Midway.alert({
                            title: "操作失败",
                            content: result.content
                        })
                    }
                    callback({
                        recordsTotal: result.content.total,
                        recordsFiltered: result.content.total,
                        data: result.content.mchtList || []
                    })
                }
            });

            $(self.$el).on("click", ".revise", function () {
                var Row = $(this).closest('tr').get(0);
                const dataArray = JSON.parse(JSON.stringify(dataTable.row(Row).data()));
                self.item = dataArray;
                $(self.$els.reviseMform).validate(self.item.valdate = {
                    async submitHandler() {
                        var form = self.$els.reviseMform;
                        var _data = serialize(form, {hash: true, disabled: true, empty: true});
                        var mchtName = dataTable.row(Row).data().mchtName;
                        var result = await Midway.confirm({
                            title: "修改结算方式",
                            content: "修改:" + mchtName + "的结算方式会导致支付失败，是否确认修改？"
                        })
                        var result = await self.$post({
                            url: "/api/v2" + projectName_Url + "/plat/mchtSett/" + self.item.mchtId,
                            data: _data,
                            method: "PUT"
                        });
                        Midway.alert({title: "操作提示", content: result.errorInfo})
                        dataTable.ajax.reload(null, false);
                        $(self.$els.reviseModal).modal('hide');
                    },
                    rules: {
                        mchtName:{
                            required: true
                        },
                        merWay:{
                            required: true
                        },
                        merId: {
                            required: true,
                            int:true,
                            min:1,
                            stringMaxLength: 20
                        },
                        merKey: {
                            required: true,
                            stringMaxLength: 50
                        }
                    }
                });
                $(self.$els.reviseModal).modal('show');
            });
        },
        beforeDestroy() {
            $(this.$el).off("click")
        }
    }
</script>
